package com.cykj.cotroller;

import com.cykj.service.EmailService;
import com.cykj.util.ImageCodeUtils;
import com.cykj.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RestController
@RequestMapping("/code")
public class CodeController {
    @Autowired
    private EmailService emailService;
    @Resource
    private RedisUtil redisUtil;

    @RequestMapping("/getcode")
    public void  getCode(HttpServletResponse response){
//        ImageCodeUtils imageCodeUtils = new ImageCodeUtils(150, 40);
//        System.out.println(imageCodeUtils.getCode());
//        session.setAttribute("code",imageCodeUtils.getCode());
//
////        System.out.println(imageCodeUtils.getCode());
//        emailService.sendMail("2145887855@qq.com",imageCodeUtils.getCode());
//        try {
//            imageCodeUtils.write(response.getOutputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        ImageCodeUtils imageCodeUtils = new ImageCodeUtils(150, 40);
        System.out.println(imageCodeUtils.getCode());
        String code=imageCodeUtils.getCode();
        redisUtil.set("code",code,60);
        System.out.println(redisUtil.get("code"));
        emailService.sendMail("2145887855@qq.com",imageCodeUtils.getCode());
        try {
            imageCodeUtils.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
